package cn.zhaoyuening.leasecloud.device.container.impl;

import cn.zhaoyuening.leasecloud.common.domain.Device;
import cn.zhaoyuening.leasecloud.common.service.middle.DeviceService;
import cn.zhaoyuening.leasecloud.device.container.ChannelContainer;
import cn.zhaoyuening.leasecloud.device.container.DeviceContainer;
import com.alibaba.dubbo.config.annotation.Reference;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Buynow on 2017/7/8.
 */
@Component
public class BaseDeviceContainer implements DeviceContainer{
//    @Reference
    private DeviceService deviceService;

    private ChannelContainer channelContainer = new BaseChannelContainer();
    private Map<String,DeviceChannelInfo> deviceChannelMap
            = new HashMap<>();
    private Map<String, String> channelDeviceMap
            = new HashMap<>();

    @Override
    public Device findById(String id) {
        if (deviceChannelMap.containsKey(id)) {
            DeviceChannelInfo deviceChannelInfo = deviceChannelMap.get(id);
            if (deviceChannelInfo != null) {
                return deviceChannelInfo.device;
            }
        }
        return null;
    }


    @Override
    public Integer queryDeviceStatus(String id) {
        if (deviceChannelMap.containsKey(id)) {
            DeviceChannelInfo deviceChannelInfo = deviceChannelMap.get(id);
            if (deviceChannelInfo != null&&deviceChannelInfo.device!=null) {
                return deviceChannelInfo.device.getStatus();
            }
        }
        return null;
    }

    @Override
    public void addDevice(String id, Integer status,Channel channel) {
        Device device = deviceService.findById(id);
        device.setStatus(status);
        channelContainer.add(channel);
        deviceChannelMap.put(device.getId(), new DeviceChannelInfo(device, channel.id().asLongText()));
        channelDeviceMap.put(channel.id().asLongText(), device.getId());
    }

    @Override
    public void setDeviceStatus(String id, Integer status) {
        deviceChannelMap.get(id).device.setStatus(status);
    }

    @Override
    public Channel getDeviceChannel(String id) {
        String channelId = deviceChannelMap.get(id).channelId;
        Channel channel = channelContainer.get(channelId);
        return channel;
    }

    @Override
    public void deviceOfflineByChannelId(String channelId) {
        channelContainer.remove(channelId);
        String deviceId = channelDeviceMap.remove(channelId);
        deviceChannelMap.get(deviceId).channelId = null;
        channelDeviceMap.remove(channelId);
    }

    @Override
    public void deviceOfflineByDeviceId(String deviceId) {
        String channelId = deviceChannelMap.get(deviceId).channelId;
        deviceChannelMap.get(deviceId).channelId = null;
        channelDeviceMap.remove(channelId);
        channelContainer.remove(channelId);
    }


    private class DeviceChannelInfo{
        private Device device;
        private String channelId;

        public DeviceChannelInfo(Device device, String channelId) {
            this.device = device;
            this.channelId = channelId;
        }

        public DeviceChannelInfo() {
        }

        public Device getDevice() {
            return device;
        }

        public DeviceChannelInfo setDevice(Device device) {
            this.device = device;
            return this;
        }

        public String getChannelId() {
            return channelId;
        }

        public DeviceChannelInfo setChannelId(String channelId) {
            this.channelId = channelId;
            return this;
        }
    }
}
